package com.chapter15.generic_.homework.homework01;

import org.junit.jupiter.api.Test;

import java.util.*;

/**
 * @author 宋鑫宇
 * @version 1.0
 */
public class Homework01 {
    public static void main(String[] args) {

    }
    @Test
    public void testList(){
        Dao<User> userDao = new Dao<>();
        userDao.save("1",new User(1,20,"jack"));
        userDao.save("2",new User(1,20,"jack"));
        System.out.println(userDao.get("1"));
        userDao.update("1",new User(2,23,"mary"));
        userDao.delete("2");
        System.out.println(userDao.list());

    }
}

class Dao<T> {
    private  Map<String, T> map = new HashMap<>();

    public void save(String id, T entity) {
        this.map.put(id, entity);
    }

    public T get(String id) {
        return this.map.get(id);
        
    }

    public void update(String id, T entity) {
        this.map.replace(id, entity);
    }

    public List<T> list() {
//        Collection<T> values = this.map.values();
//        ArrayList<T> objects = new ArrayList<>();
//        for (T o :values) {
//            objects.add(o);
//        }
//        return objects;
        Set<String> strings = this.map.keySet();
        List strings1 = new ArrayList<>();
        for (String s : strings) {
            strings1.add(get(s));
        }

        return strings1;
    }

    public void delete(String id) {
        this.map.remove(id);
    }
}

class User{
    private int id;
    private int age;
    private String name;

    public User(int id, int age, String name) {
        this.id = id;
        this.age = age;
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", age=" + age +
                ", name='" + name + '\'' +
                '}';
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

